package cn.glutnn.controller;


import cn.glutnn.entity.Deliveryaddress;
import cn.glutnn.entity.Result;
import cn.glutnn.service.DeliveryaddressService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 送货地址表(Deliveryaddress)表控制层
 *
 * @author msh
 * @since 2024-12-12 16:53:44
 */
@RestController
@RequestMapping("/deliveryaddress")
public class DeliveryaddressController {
    /**
     * 服务对象
     */
    @Autowired
    private DeliveryaddressService deliveryaddressService;

    @PostMapping
    public Result<String> addDeliveryAddress(@RequestBody Deliveryaddress deliveryaddress) {
        deliveryaddressService.save(deliveryaddress);
        return Result.success("添加成功");
    }

    @GetMapping("/listDeliveryAddressByUserId/{userId}")
    public Result<List<Deliveryaddress>> listDeliveryAddressByUserId(@PathVariable("userId") Integer userId) {
        LambdaQueryWrapper<Deliveryaddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Deliveryaddress::getUserId, userId);
        return Result.success(deliveryaddressService.list(wrapper));
    }

    @GetMapping("/{id}")
    public Result<Deliveryaddress> getDeliveryAddressById(@PathVariable Integer id) {
        return Result.success(deliveryaddressService.getById(id));
    }

    @PutMapping
    public Result<String> updateDeliveryAddress(@RequestBody Deliveryaddress deliveryaddress) {
        deliveryaddressService.updateById(deliveryaddress);
        return Result.success("更新成功");
    }

    @DeleteMapping("/{id}")
    public Result<String> deleteDeliveryAddress(@PathVariable("id") @RequestBody Integer id) {
        deliveryaddressService.removeById(id);
        return Result.success("删除成功");
    }
}

